package com.uppalz.fundmanager.beans;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Zafar
 */
public class LoginBean {

    private final String no_user = "<< Not signed in! >>";
    private UserService userService;
    private User user;

    public LoginBean() {
    }

    public boolean isLoggedIn() {
        userService = UserServiceFactory.getUserService();
        if (userService == null) {
            System.err.println("UserService is null !!!!!!!!!! ");
        }

        user = userService.getCurrentUser();

        return user != null;
    }

    public String getUserName() {
        if (user != null) {
            return user.getNickname();
        } else {
            return no_user;
        }
    }

    public String getEmail() {
        if (user != null) {
            return user.getEmail();
        } else {
            return no_user;
        }
    }

    public void redirect(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.sendRedirect(userService.createLoginURL(request.getRequestURI()));
    }

}
